<template>

  <div id="login" class="login" style="text-align: center">
    <div class="background">
      <img :src="imgSrc" width="100%" height="100%" alt=""/>
    </div>
    <div class="wrap">
      <h1>登 录</h1>
      <el-form :model="form" ref="form" class="form">
        <el-form-item prop="username">
          <el-input placeholder="用户名或邮箱" type="username" v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item id="password" prop="password">
          <el-input
              placeholder="密码"
              show-password
              type="password"
              v-model="form.password"
              autocomplete="off"
              @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn_login">
          <el-button type="primary" @click="login">登&nbsp;&nbsp;录</el-button>
        </el-form-item>
      </el-form>
      <div class="suffix">
        <p @click="toRegister">注册帐号</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "NewLogin",
  data() {
    return {
      // eslint-disable-next-line no-undef
      imgSrc: require('@/assets/background1.jpg'),
      form: {
        username: '',
        password: '',
      }
    }
  },
  methods: {
    login: function () {
      var useradmin;
      var userBan;
      // 检查表单是否有填写内容
      if (this.form.username === '' || this.form.password === '') {
        this.$message.warning("请输入用户名和密码！");
        return;
      }

      this.$axios({
        method: 'post',           /* 指明请求方式，可以是 get 或 post */
        url: '/user/login',       /* 指明后端 api 路径，由于在 main.js 已指定根路径，因此在此处只需写相对路由 */
        data: ({      /* 需要向后端传输的数据，此处使用 qs.stringify 将 json 数据序列化以发送后端 */
          userAccount: this.form.username,
          userPassword: this.form.password
        })
      })
          .then(res => {
            switch (res.data.code) {
              case 0:
                this.$message.success("登录成功！");
                /* 将后端返回的 user 信息使用 vuex 存储起来 */
                this.$store.state.isLogin = true;
                useradmin = (res.data.data.userRole === 1);
                userBan=(res.data.data.userRole===3)
                this.$store.dispatch('saveUserInfo', {
                  user: {
                    'username': res.data.data.userAccount,
                    'userID': res.data.data.id,
                    'userIsAdmin': useradmin,
                    'userIsBan':userBan
                  }
                });
                this.$store.state.isAdmin = useradmin
                  this.$store.state.isBan=userBan
                /* 从 localStorage 中读取 preRoute 键对应的值 */
                // eslint-disable-next-line no-case-declarations
                const history_pth = localStorage.getItem('preRoute');
                /* 若保存的路由为空或为注册路由，则跳转首页；否则跳转前路由（setTimeout表示1000ms后执行） */
                setTimeout(() => {
                  if (history_pth == null || history_pth === '/register') {
                    this.$router.push('/');
                  } else {
                    this.$router.push({ path: history_pth });
                  }
                }, 1000);
                break;
              case 40000:
                this.$message.error("用户名或密码错误");
                break;
            }
          })
          .catch(err => {
            console.log(err);         /* 若出现异常则在终端输出相关信息 */
          })
    },
    toRegister: function () {
      // 跳转注册的路由
      this.$router.push('/register');
    }
  }
}
</script>

<style scoped>
.background {
  width: 100%;
  height: 100%; /**宽高100%是为了图片铺满屏幕 */
  top: 8%;
  z-index: -1;
  position: absolute;

}

#login {
  font-family: 'Noto Serif SC', serif;
  margin-top: 60px;
  position: center;
}

#login >>> .el-input__inner {
  font-family: 'Noto Serif SC', serif;
}

#login .bgbox {
  display: block;
  opacity: 1;
  z-index: -3;
  position: center;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: opacity 1s,transform .25s,filter .25s;
  backface-visibility: hidden;
}
#login .logo {
  cursor: pointer;
  overflow: hidden;
  height: 150px;
}
#login .wrap {
  width: 300px;
  height: 315px;
  padding: 0 25px 0 25px;
  line-height: 40px;
  position: relative;
  display: inline-block;
  background-color: rgba(255, 255, 255, 0.85);
  border-radius: 20px;
}
#login .btn_login {
  margin-top: 25px;
  text-align: center;
}
#login .btn_login button{
  line-height: 10px;
  font-family: 'Noto Serif SC', serif;
  width: 100%;
  height: 38px;
}
#login .suffix {
  font-size:14px;
  line-height:10px;
  color:#999;
  cursor: pointer;
  float:right;
}
</style>
